home *** CD-ROM | disk | FTP | other *** search
-
- #include <windows.h>
- #include <commctrl.h>
- #include "sort.h"
-
-
- #if defined (WIN32)
- #define IS_WIN32 TRUE
- #else
- #define IS_WIN32 FALSE
- #endif
-
- #define IS_NT IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
- #define IS_WIN32S IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
- #define IS_WIN95 (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
-
- HINSTANCE hInst; // current instance
-
- LPCTSTR lpszAppName = "App";
- LPCTSTR lpszTitle = "TreeView_SortChildren()";
-
-
- BOOL RegisterWin95( CONST WNDCLASS* lpwc );
-
-
- int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
- HWND hWnd;
- WNDCLASS wc;
-
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = (WNDPROC)WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon (hInstance, lpszAppName);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszMenuName = lpszAppName;
- wc.lpszClassName = lpszAppName;
-
- if ( IS_WIN95 )
- {
- if ( !RegisterWin95( &wc ) )
- return( FALSE );
- }
- else if ( !RegisterClass( &wc ) )
- return( FALSE );
-
- hInst = hInstance;
-
- hWnd = CreateWindow( lpszAppName,
- lpszTitle,
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0,
- CW_USEDEFAULT, 0,
- NULL,
- NULL,
- hInstance,
- NULL
- );
-
- if ( !hWnd )
- return( FALSE );
-
- ShowWindow( hWnd, nCmdShow );
- UpdateWindow( hWnd );
-
- while( GetMessage( &msg, NULL, 0, 0) )
- {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
-
- return( msg.wParam );
- }
-
-
- BOOL RegisterWin95( CONST WNDCLASS* lpwc )
- {
- WNDCLASSEX wcex;
-
- wcex.style = lpwc->style;
- wcex.lpfnWndProc = lpwc->lpfnWndProc;
- wcex.cbClsExtra = lpwc->cbClsExtra;
- wcex.cbWndExtra = lpwc->cbWndExtra;
- wcex.hInstance = lpwc->hInstance;
- wcex.hIcon = lpwc->hIcon;
- wcex.hCursor = lpwc->hCursor;
- wcex.hbrBackground = lpwc->hbrBackground;
- wcex.lpszMenuName = lpwc->lpszMenuName;
- wcex.lpszClassName = lpwc->lpszClassName;
-
- // Added elements for Windows 95.
- //...............................
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName,
- IMAGE_ICON, 16, 16,
- LR_DEFAULTCOLOR );
-
- return RegisterClassEx( &wcex );
- }
-
- VOID AddItemsToTree( HWND hTree );
- int CALLBACK SortCompare( LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort );
-
- #define IMAGESIZE 16
-
- LPCTSTR lpszData[4] = { "Circle", "Rectangle", "Cross", "Check" };
-
- LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- static HWND hTree = NULL;
-
- switch( uMsg )
- {
- case WM_CREATE :
- {
- HIMAGELIST hImage;
-
- InitCommonControls();
-
- hImage = ImageList_Create( IMAGESIZE, IMAGESIZE,
- ILC_COLOR4 | ILC_MASK, 4, 1 );
-
- hTree = CreateWindowEx( WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
- WS_CHILD | WS_BORDER | WS_VISIBLE |
- WS_VSCROLL | TVS_HASLINES |
- TVS_HASBUTTONS | TVS_LINESATROOT |
- TVS_DISABLEDRAGDROP,
- 10, 100, 100, 100,
- hWnd,
- (HMENU)1,
- hInst,
- NULL);
-
-
- if ( hTree )
- {
- int i;
-
- // Associate the image list with the tree view.
- //.............................................
- TreeView_SetImageList( hTree, hImage, TVSIL_NORMAL );
-
- // Add the images to the tree view image list.
- //............................................
- for ( i=0; i<4; i++ )
- ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
-
- AddItemsToTree( hTree );
- }
- }
- break;
-
-
- case WM_SIZE :
- if ( hTree )
- MoveWindow( hTree,0,0, LOWORD(lParam), HIWORD(lParam), FALSE );
- break;
-
-
- case WM_COMMAND :
- switch( LOWORD( wParam ) )
- {
- case IDM_SORTALPHA :
- TreeView_SortChildren( hTree, NULL, 0 );
- break;
-
- case IDM_SORTNUMERIC :
- {
- TV_SORTCB tvs;
-
- tvs.hParent = NULL;
- tvs.lpfnCompare = SortCompare;
- tvs.lParam = 0;
-
- TreeView_SortChildrenCB( hTree, &tvs, 0 );
- }
- break;
-
- case IDM_ABOUT :
- DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
- break;
-
- case IDM_EXIT :
- DestroyWindow( hWnd );
- break;
- }
- break;
-
- case WM_DESTROY :
-
- if ( TreeView_GetImageList( hTree, TVSIL_NORMAL ) )
- ImageList_Destroy( TreeView_GetImageList( hTree, TVSIL_NORMAL ) );
-
- PostQuitMessage(0);
- break;
-
- default :
- return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
- }
-
- return( 0L );
- }
-
-
- int CALLBACK SortCompare( LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort )
- {
- if ( lParam1 < lParam2 )
- return( -1 );
-
- if ( lParam1 > lParam2 )
- return( 1 );
-
- return( 0 );
- }
-
-
- VOID AddItemsToTree( HWND hTree )
- {
- char szBuf[40];
- int i;
- TV_INSERTSTRUCT tv;
-
- tv.hInsertAfter = TVI_LAST;
- tv.item.mask = TVIF_TEXT | TVIF_IMAGE |
- TVIF_SELECTEDIMAGE | TVIF_PARAM;
-
- // Add the items to the tree view.
- //................................
- for ( i=0; i<4; i++ )
- {
- wsprintf( szBuf, "%s [%d]", lpszData[i], i+1 );
-
- tv.hParent = TVI_ROOT;
- tv.item.pszText = szBuf;
- tv.item.iImage = i;
- tv.item.iSelectedImage = i;
- tv.item.lParam = i+1;
-
- TreeView_InsertItem( hTree, &tv );
- }
- }
-
-
- LRESULT CALLBACK About( HWND hDlg,
- UINT message,
- WPARAM wParam,
- LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- return (TRUE);
-
- case WM_COMMAND:
- if ( LOWORD(wParam) == IDOK
- || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, TRUE);
- return (TRUE);
- }
- break;
- }
-
- return (FALSE);
- }
-
-
-